//
//  WTDataBaseHandle.m
//  艾听歌
//
//  Created by dllo on 16/6/4.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "WTDataBaseHandle.h"



@interface WTDataBaseHandle ()

@property(nonatomic, strong)FMDatabaseQueue *myQueue;

@end


@implementation WTDataBaseHandle

+(WTDataBaseHandle *)shareDataFMDBHandle{
    
    
    static WTDataBaseHandle *data = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        data = [[WTDataBaseHandle alloc]init];
    });
    
    return data;
}


-(void)openDB{
    
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    
    NSLog(@"%@",path);
    
    NSString *dataPath = [path stringByAppendingPathComponent:@"DouFMDB.sqlite"];
    
    
    self.myQueue = [FMDatabaseQueue databaseQueueWithPath:dataPath];
    
    [self.myQueue inDatabase:^(FMDatabase *db) {
        
        BOOL result = [db open];
        
        if (result) {
            NSLog(@"数据库打开成功");
        }else{
            
            NSLog(@"数据库打开失败");
        }
        
        
    }];
    
    
}

//用户表

-(void)createUserTable{
    
    [self.myQueue inDatabase:^(FMDatabase *db) {
        
        BOOL result = [db executeUpdate:@"create table if not exists userTable(id integer primary key autoincrement, userName text, userPwd text)"];
        if (result) {
            NSLog(@"创建用户表成功");
        }else{
            NSLog(@"创建用户表失败");
        }
        
    }];
    
}


-(void)insertUserWithUserModel:(UserModel *)user{
    
    [self.myQueue inDatabase:^(FMDatabase *db) {
        
        NSString *sql = [NSString stringWithFormat:@"insert into userTable(userName, userPwd) values('%@', '%@')",user.userName, user.userPwd];
        
        BOOL result = [db executeUpdate:sql];
        if (result) {
            
            NSLog(@"插入用户数据成功");
        }else{
            
            NSLog(@"插入用户数据失败");
        }
        
    }];
    
}

-(NSMutableArray *)selectUserWithCompare:(BOOL)compare{
    
    NSMutableArray *array1 = [NSMutableArray array];
    NSMutableArray *array2 = [NSMutableArray array];
    
    [self.myQueue inDatabase:^(FMDatabase *db) {
        
        NSString *sql = [NSString stringWithFormat:@"select * from userTable"];
        FMResultSet *result = [db executeQuery:sql];
        
        while ([result next]) {
            UserModel *model = [[UserModel alloc]init];
            model.userName = [result objectForColumnName:@"userName"];
            model.userPwd = [result objectForColumnName:@"userPwd"];
            [array1 addObject:model.userName];
            [array2 addObject:model];
            
        }
        
        
    }];
    
    if (compare == NO) {
        
        return array1;
    }else{
        
        return array2;
    }
    
}
// 歌曲播放页面
- (void)openMusicPlay
{
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    
    NSLog(@"%@",path);
    
    NSString *dataPath = [path stringByAppendingPathComponent:@"MusicPlay.sqlite"];
    
    
    self.myQueue = [FMDatabaseQueue databaseQueueWithPath:dataPath];
    
    [self.myQueue inDatabase:^(FMDatabase *db) {
        
        BOOL result = [db open];
        
        if (result) {
            NSLog(@"音乐数据库打开成功");
        }else{
            
            NSLog(@"音乐数据库打开失败");
        }
        
        
    }];

}
- (void)createMucicPlay
{
    [self.myQueue inDatabase:^(FMDatabase *db) {
        
        BOOL result = [db executeUpdate:@"create table if not exists MusicPlay(id integer primary key autoincrement, name text, songId text, singerId text, singerName text, albumName text, picUrl text, alias text)"];
        if (result) {
            NSLog(@"创建音乐用户表成功");
        }else{
            NSLog(@"创建音乐用户表失败");
        }
        
    }];

}

//- (void)createMucicPlay
//{
//    [self.myQueue inDatabase:^(FMDatabase *db) {
//        
//        BOOL result = [db executeUpdate:@"create table if not exists MusicPlay(id integer primary key autoincrement, name text, songId text, singerId text, singerName text, albumName text, urlList text, picUrl text, alias text)"];
//        if (result) {
//            NSLog(@"创建用户表成功");
//        }else{
//            NSLog(@"创建用户表失败");
//        }
//        
//    }];
//    
//}



- (void)inserterWithModel:(SongListModel *)songModel
{
    [self.myQueue inDatabase:^(FMDatabase *db) {
        
        NSString *sql = [NSString stringWithFormat:@"insert into MusicPlay(name, songId, singerId, singerName, albumName, picUrl, alias) values('%@', '%@', '%@', '%@', '%@', '%@', '%@')",songModel.name, songModel.songId, songModel.singerId, songModel.singerName, songModel.albumName, songModel.picUrl, songModel.alias];
        
        BOOL result = [db executeUpdate:sql];
        if (result) {
            
            NSLog(@"插入音乐用户数据成功");
        }else{
            
            NSLog(@"插入音乐用户数据失败");
        }
        
    }];

}

//- (void)inserterWithModel:(SongListModel *)songModel
//{
//    [self.myQueue inDatabase:^(FMDatabase *db) {
//        
//        NSString *sql = [NSString stringWithFormat:@"insert into MusicPlay(name, songId, singerId, singerName, albumName, urlList, picUrl, alias) values('%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@')",songModel.name, songModel.songId, songModel.singerId, songModel.singerName, songModel.albumName, songModel.urlList, songModel.picUrl, songModel.alias];
//        
//        BOOL result = [db executeUpdate:sql];
//        if (result) {
//            
//            NSLog(@"插入用户数据成功");
//        }else{
//            
//            NSLog(@"插入用户数据失败");
//        }
//        
//    }];
//    
//}


//查询所有数据   compare == NO 用作注册判断


-(NSMutableArray *)selectMusicPlaywithCompare:(BOOL)compare
{
    
     NSMutableArray *nameArray = [NSMutableArray array];//用作判断此攻略是否收藏过了
       NSMutableArray *Array = [NSMutableArray array];
    
    
    [self.myQueue inDatabase:^(FMDatabase *db) {
        NSString *sql = [NSString stringWithFormat:@"select * from MusicPlay"];
        FMResultSet *result =  [db executeQuery:sql];
        while ([result next]) {
            SongListModel *model = [[SongListModel alloc]init];
            model.singerId = [result objectForColumnName:@"singerId"];
            model.name = [result objectForColumnName:@"name"];
            model.songId
            = [result objectForColumnName:@"songId"];
            model.singerName = [result objectForColumnName:@"singerName"];
            model.albumName = [result objectForColumnName:@"albumName"];
            //model.urlList = [result objectForColumnName:@"urlList"];
            model.picUrl = [result objectForColumnName:@"picUrl"];
            model.alias = [result objectForColumnName:@"alias"];
            
             [nameArray addObject:model.name];
            [Array addObject:model];
            
        }
    }];
    if (compare == NO) {
        
        return nameArray;
    }else{
        
        return Array;
    }
    
}

//-(NSMutableArray *)selectMusicPlaywithCompare:(BOOL)compare
//{
//    
//    NSMutableArray *nameArray = [NSMutableArray array];//用作判断此攻略是否收藏过了
//    NSMutableArray *Array = [NSMutableArray array];
//    
//    
//    [self.myQueue inDatabase:^(FMDatabase *db) {
//        NSString *sql = [NSString stringWithFormat:@"select * from MusicPlay"];
//        FMResultSet *result =  [db executeQuery:sql];
//        while ([result next]) {
//            SongListModel *model = [[SongListModel alloc]init];
//            model.singerId = [result objectForColumnName:@"singerId"];
//            model.name = [result objectForColumnName:@"name"];
//            model.singerId = [result objectForColumnName:@"singerId"];
//            model.singerName = [result objectForColumnName:@"singerName"];
//            model.albumName = [result objectForColumnName:@"albumName"];
//            model.urlList = [result objectForColumnName:@"urlList"];
//            model.picUrl = [result objectForColumnName:@"picUrl"];
//            model.alias = [result objectForColumnName:@"alias"];
//            
//            [nameArray addObject:model.name];
//            [Array addObject:model];
//            
//        }
//    }];
//    if (compare == NO) {
//        
//        return nameArray;
//    }else{
//        
//        return Array;
//    }
//    
//}


//删除数据 根据用户名 和所点的id

-(void)deleteMusicId:(NSNumber *)songId{
    NSString *sql = [NSString stringWithFormat:@"delete from MusicPlay where songId = '%@'",songId];
    [self.myQueue inDatabase:^(FMDatabase *db) {
        BOOL result = [db executeUpdate:sql];
        if (result) {
            NSLog(@"删除音乐推荐成功");
        } else {
            NSLog(@"删除音乐推荐失败");
        }
    }];
    
}



@end
